package com.jie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.dao.AllCatDao;
import com.jie.entity.Cat;
import com.jie.mapper.CatMapper;
import com.jie.service.CatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.entity.QuerInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-01-16
 */
@Service
public class CatServiceImpl extends ServiceImpl<CatMapper, Cat> implements CatService {
    @Autowired
    private CatMapper catMapper;
    @Override
    public Page<AllCatDao> allcat(QuerInfo queryInfo) {
        Page<AllCatDao> allCatDaoPage=new Page<>();
        Page page=new Page<>(queryInfo.getPagenum(), queryInfo.getPagesize());
        LambdaQueryWrapper<Cat> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cat::getCatLevel,1);
        catMapper.selectPage(page,queryWrapper);
        List<AllCatDao> allCatDaos=new ArrayList<>();
        for (Object record : page.getRecords()) {
            AllCatDao allCatDao = JSON.parseObject(JSON.toJSONString(record), AllCatDao.class);
            allCatDaos.add(allCatDao);
        }
        allCatDaoPage.setTotal(page.getTotal());
        if(queryInfo.getType()==1){
            allCatDaoPage.setRecords(allCatDaos);
            return allCatDaoPage;
        }else {
            for (AllCatDao catDao:allCatDaos) {
                LambdaQueryWrapper<Cat> queryWrapper1=new LambdaQueryWrapper<>();
                queryWrapper1.eq(Cat::getCatPid,catDao.getCatId());
                List<Cat> children = catMapper.selectList(queryWrapper1);
                catDao.setChildren(children);

            }
            allCatDaoPage.setRecords(allCatDaos);
            return allCatDaoPage;
        }

    }

    @Override
    public List<AllCatDao> allcat() {
        LambdaQueryWrapper<Cat> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cat::getCatLevel,1);
        List<Cat> cats = catMapper.selectList(queryWrapper);
        List<AllCatDao> allCatDaos = new ArrayList<>();
        for (Cat cat:cats) {
            AllCatDao allCatDao = new AllCatDao();
            BeanUtils.copyProperties(cat,allCatDao);
            LambdaQueryWrapper<Cat> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(Cat::getCatPid,allCatDao.getCatId());
            List<Cat> children = catMapper.selectList(queryWrapper1);
            allCatDao.setChildren(children);
            allCatDaos.add(allCatDao);
        }
        return allCatDaos;
    }

    @Override
    public List<AllCatDao> parentcat(Integer type) {
        LambdaQueryWrapper<Cat> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cat::getCatLevel,type);
        List<Cat> cats = catMapper.selectList(queryWrapper);
        List<AllCatDao> allCatDaos = new ArrayList<>();
        for (Cat cat:cats) {
            AllCatDao allCatDao = new AllCatDao();
            BeanUtils.copyProperties(cat,allCatDao);
            allCatDaos.add(allCatDao);
        }
        return allCatDaos;
    }
}
